package cn.school.web;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cn.school.entity.Admin;
import cn.school.service.AdminService;

/**
 * Servlet implementation class LoginServlet
 */
@WebServlet(value = "/LoginServlet", initParams = { @WebInitParam(name = "encoding", value = "utf-8") })
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	private String encoding;

	AdminService adminService = new AdminService();

	public LoginServlet() {
		super();
		System.out.println("LoginServlet 被实例化了");
	}

	@Override
	protected void service(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException {
		System.out.println("进入了服务方法：" + arg0.getMethod() + "请求");
		super.service(arg0, arg1);
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		System.out.println("当前的字符集是：" + encoding);

		// super.getServletContext().getRealPath("")
		// response.addCookie(arg0);
		// request.getCookies();

		// response.getWriter().append("Served at: ").append(request.getContextPath());
		request.setCharacterEncoding(encoding);
		String name = request.getParameter("username");
		String pass = request.getParameter("pass");

		Admin admin = adminService.login(name, pass);

		// if(name.startsWith("shuaige")&&pass.equals("123456")){
		if (admin != null) {
			// session记录你的信息
			HttpSession session = request.getSession();
			session.setAttribute("USER_LOGIN", admin);
			String keep = request.getParameter("keeppassincookie");
			if ("on".equals(keep)) {
				// cookie
				Cookie u1 = new Cookie("username", name);
				Cookie p1 = new Cookie("userpass", pass);
				u1.setMaxAge(86400 * 7);
				p1.setMaxAge(86400 * 7);
				response.addCookie(u1);
				response.addCookie(p1);
			}
			// request.getRequestDispatcher("myweb/success.jsp").forward(request, response);
			response.sendRedirect("success.jsp");
			return;
		}
		response.sendRedirect("login.jsp?msg=error input");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request, response);
	}

	@Override
	public void init() throws ServletException {
		// super.getServletConfig()
		super.init();
		encoding = super.getServletConfig().getInitParameter("encoding");
		System.out.println("LoginServlet初始化完成");
	}

	@Override
	public void destroy() {
		// TODO 自动生成的方法存根
		super.destroy();
		System.out.println("LoginServlet被销毁了");
	}
}
